import fs from "fs"
import { fileURLToPath, pathToFileURL } from "url"
export const getControllerUrl = function () {
    let controllerUrl = new URL("../controller", import.meta.url).href
    // console.log(controllerUrl);
    
    let controllerFile = fileURLToPath(controllerUrl)
    // console.log(controllerFile);

    let url = fs.readdirSync(controllerFile).filter(item => item.endsWith("Controller.js")).map(item => `${controllerUrl}/${item}`)
    // console.log(url);
    
    return url
}
export const esgController = function (router, controllerFiles) {
    controllerFiles.forEach(
        async files => {
            let { default: obj } = await import(files)
            obj.forEach(
                obj => {
                    if (obj.method === "get") {
                        router.get(obj.url, obj.fn)
                    }
                    if (obj.method === "post") {
                        router.post(obj.url, obj.fn)
                    }
                    if (obj.method === "put") {
                        router.put(obj.url, obj.fn)
                    }
                    if (obj.method === "del") {
                        router.delete(obj.url, obj.fn)
                    }
                }
            )
        }
    )
}